
import mirny, re, gettext, os, sys, imp
import Pyro, Pyro.core, Pyro.naming, Pyro.protocol
from Pyro.errors import PyroError,NamingError

pyro_group = ':mirny'
Pyro.config.PYRO_NS_DEFAULTGROUP=pyro_group
Pyro.config.PYRO_STORAGE='/tmp'


class Manager:
	ns = Pyro.naming.NameServerLocator().getNS()
	def __init__(self):
		try:
			self.ns.createGroup(pyro_group)
		except NamingError:
			pass
	def list_plugins(self):
		return self.ns.list(pyro_group)
	def load_plugin(self, plugin):
		try:
			modinfo = imp.find_module(plugin, ['plugins/'+k for k in ['input','carve','dissect','extract','filter','output']])
			mod = imp.load_module('pluginmodule', *modinfo)
			plugclasses = [mod.__dict__[c] for c,v in mod.__dict__.iteritems() if type(mod.__dict__[c]) is class 
			
		except Exception, e:
			print str(e)


#class StreamMatcher:
#	def __init__(self, regex, stream, bufsize=102400):
#		self.regex = re.compile(regex)
#		self.stream = stream
#		self.bufsize = bufsize
#		self.snapsize = bufsize/2+1
#		self.buf = self.stream.read(bufsize)
#		if len(self.buf) == 0:
#			raise EOFError('End of stream')
#		self.firstmatch = self.regex.search(self.buf)
#	def match(self):
#		if self.firstmatch:
#			m = self.firstmatch
#			self.firstmatch = None
#			return m
#		rbuf = self.stream.read(self.snapsize)
#		if len(rbuf) == 0:
#			raise EOFError('End of stream')
#		self.buf = self.buf[self.snapsize:] + rbuf
#		return self.regex.search(self.buf)

